package com.example.win.android_banduyujia.peopleactivity.ui.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.example.win.android_banduyujia.R;
import com.example.win.android_banduyujia.peopleactivity.ui.item.PhotoItem;
import com.example.win.android_banduyujia.peopleactivity.ui.model.PhotoModel;
import com.example.win.android_banduyujia.peopleactivity.ui.util.CommonUtils;

import java.util.ArrayList;

/**
 * Created by Win on 2015/9/17.
 */
public class PhotoSelectorAdapter extends MBaseAdapter<PhotoModel> {

    private int itemWidth;
    private int horizentalNum = 3;
    private PhotoItem.onPhotoItemCheckedListener listener;
    private ViewGroup.LayoutParams  itemLayoutParams;
    private PhotoItem.onItemClickListener mCallback;
    private View.OnClickListener cameraListener;

    private PhotoSelectorAdapter(Context context, ArrayList<PhotoModel> models) {
        super(context, models);
    }

    public PhotoSelectorAdapter(Context context, ArrayList<PhotoModel> models,
                 int screenWidth, PhotoItem.onPhotoItemCheckedListener listener,
                                PhotoItem.onItemClickListener mCallback, View.OnClickListener cameraListener) {
         this(context, models);
         setItemWidth(screenWidth);
         this.listener = listener;
         this.mCallback = mCallback;
         this.cameraListener = cameraListener;
    }

    /** 设置每一个Item的宽高 */
    public void setItemWidth(int screenWidth) {
        int horizentalSpce = context.getResources().getDimensionPixelSize(R.dimen.sticky_item_horizontalSpacing);
        this.itemWidth = (screenWidth - (horizentalSpce * (horizentalNum -1))) / horizentalNum;
        this.itemLayoutParams = new ViewGroup.LayoutParams(itemWidth, itemWidth);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        PhotoItem item;
        TextView  tvCamera;
        if(position == 0 && CommonUtils.isNull(models.get(position).getOriginalPath())){  // 当时第一个时，显示按钮
             if(convertView == null || !(convertView instanceof TextView)){
                 tvCamera=(TextView) LayoutInflater.from(context).inflate(R.layout.view_camera, null);
                 tvCamera.setWidth(itemWidth);
                 tvCamera.setHeight(itemWidth);
                 convertView = tvCamera;
             }
            convertView.setOnClickListener(cameraListener);
        }else{  // 显示图片
             if(convertView == null || !(convertView instanceof PhotoItem)){
                 item = new PhotoItem(context, listener);
                 item.setLayoutParams(itemLayoutParams);
                 convertView = item;
             }else{
                 item = (PhotoItem) convertView;
             }
            item.setImageDrawable(models.get(position));
            item.setSelected(models.get(position).isChecked());
            item.setOnClickListener(mCallback, position);
        }
        return convertView;
    }
}
